<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['firemen:TheoryExams:edit']">修改</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="TheoryExamsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="name" />
      <!-- 自定义列：考核概览 -->
      <el-table-column label="考核概览" align="center" width="1000">
        <template slot-scope="{ row }">
          <template v-if="row.fmTheoryExamScoresList && row.fmTheoryExamScoresList.length > 0">
            <el-tag style="margin-left: 10px;" v-for="(examScore, index) in row.fmTheoryExamScoresList" :key="index"
              :type="examScore.isQualified ? 'success' : 'danger'">
              {{ examScore.subject }}: {{ examScore.score }} ({{ examScore.isQualified ? '合格' : '不合格' }})
            </el-tag>

          </template>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['firemen:TheoryExams:edit']">修改</el-button>
          <!-- 禁止删除 -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['firemen:TheoryExams:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改业务理论考核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-divider content-position="center">业务理论考试成绩信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini"
              @click="handleAddFmTheoryExamScores">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini"
              @click="handleDeleteFmTheoryExamScores">删除</el-button>
          </el-col>
        </el-row>

        <!-- <el-row v-for="(item,i) in modelScoresList">
          <el-col :span="12">
            <template v-if="fmTheoryExamScoresList[i]!=null">
            <el-form-item :label="fmTheoryExamScoresList[i].subject">
                <el-input v-model="fmTheoryExamScoresList[i].score"></el-input>
            </el-form-item>
          </template>
          <template v-else>
            <el-form-item :label="item.subject">
                <el-input v-model="item.score"></el-input>
            </el-form-item>
          </template>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row> -->

        <el-table :data="fmTheoryExamScoresList" :row-class-name="rowFmTheoryExamScoresIndex"
          @selection-change="handleFmTheoryExamScoresSelectionChange" ref="fmTheoryExamScores">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="科目" prop="subject" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.subject" placeholder="请输入科目" />
            </template>
          </el-table-column>
          <el-table-column label="分数" prop="score" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.score" placeholder="请输入分数" />
            </template>
          </el-table-column>
          <el-table-column label="是否合格" prop="isQualified" width="150">
            <template slot-scope="scope">
              <el-radio-group v-model="scope.row.isQualified">
                <el-radio :label="1">合格</el-radio>
                <el-radio :label="0">不合格</el-radio>
              </el-radio-group>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTheoryExams, getTheoryExams, delTheoryExams, addTheoryExams, updateTheoryExams } from "@/api/firemen/TheoryExams";

export default {
  name: "TheoryExams",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedFmTheoryExamScores: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业务理论考核表格数据
      TheoryExamsList: [],
      // 业务理论考试成绩表格数据
      fmTheoryExamScoresList: [],
      modelScoresList:
        [
          {
            // "createBy": null,
            // "createTime": null,
            // "updateBy": null,
            // "updateTime": null,
            // "remark": null,
            // "id": 2,
            // "firefighterId": 1,
            "subject": "政治理论",
            "score": "",
            "isQualified": 1
          },
          {
            "subject": "条令法纪",
            "score": "",
            "isQualified": 1
          },
          {
            "subject": "灭火救援 实用理论",
            "score": "",
            "isQualified": 1
          },
          {
            "subject": "队列考核",
            "score": "",
            "isQualified": 1
          }
        ],


      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询业务理论考核列表 */
    getList() {
      this.loading = true;
      listTheoryExams(this.queryParams).then(response => {
        this.TheoryExamsList = response.rows;
        console.log(response);

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        gender: null,
        dateOfBirth: null,
        educationLevel: null,
        politicalStatus: null,
        identityType: null,
        entryPicture: null
      };
      this.fmTheoryExamScoresList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加业务理论考核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTheoryExams(id).then(response => {
        this.form = response.data;
        this.fmTheoryExamScoresList = response.data.fmTheoryExamScoresList;
        console.log(this.fmTheoryExamScoresList);

        this.open = true;
        this.title = "修改业务理论考核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.fmTheoryExamScoresList = this.fmTheoryExamScoresList;
          console.log(this.form);

          if (this.form.id != null) {
            updateTheoryExams(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTheoryExams(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除业务理论考核编号为"' + ids + '"的数据项？').then(function () {
        return delTheoryExams(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 业务理论考试成绩序号 */
    rowFmTheoryExamScoresIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 业务理论考试成绩添加按钮操作 */
    handleAddFmTheoryExamScores() {
      let obj = {};
      obj.subject = "";
      obj.score = "";
      obj.isQualified = "";
      this.fmTheoryExamScoresList.push(obj);
    },
    /** 业务理论考试成绩删除按钮操作 */
    handleDeleteFmTheoryExamScores() {
      if (this.checkedFmTheoryExamScores.length == 0) {
        this.$modal.msgError("请先选择要删除的业务理论考试成绩数据");
      } else {
        const fmTheoryExamScoresList = this.fmTheoryExamScoresList;
        const checkedFmTheoryExamScores = this.checkedFmTheoryExamScores;
        this.fmTheoryExamScoresList = fmTheoryExamScoresList.filter(function (item) {
          return checkedFmTheoryExamScores.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleFmTheoryExamScoresSelectionChange(selection) {
      this.checkedFmTheoryExamScores = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('firemen/TheoryExams/export', {
        ...this.queryParams
      }, `TheoryExams_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.el-radio-group input[aria-hidden="true"] {
  display: none !important
 }
</style>
